package project1.controller;

import com.google.gson.Gson;

import project1.model.Admin;
import project1.model.Result;
import project1.model.Type;
import project1.model.bo.GoodsBO;
import project1.model.bo.SpecBO;
import project1.model.bo.TypeBO;
import project1.service.GoodesServiceImpl;
import project1.service.GoodsService;
import project1.utils.FileUpLoadUtils;
import project1.view.TypeGoodsVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import project1.utils.HtttpUtils;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/goods/*")
public class GoodsServlet extends HttpServlet {
    private GoodsService goodsService = new GoodesServiceImpl();
    private Gson gson = new Gson();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if ("imgUpload".equals(action)){
          imgUpload(request,response);
       }else if("addGoods".equals(action)){
            addGoods(request,response); 
        }
        else if ("addType".equals(action)){
            addType(request,response);
        }
    }

    /**
     * 增加一个商品分类
     * @param request
     * @param response
     */
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HtttpUtils.getRequestBody(request);
        TypeBO typeBO = gson.fromJson(requestBody,TypeBO.class);


        System.out.println();
        goodsService.addType(typeBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 保存商品的业务逻辑
     * 1.获取请求体的参数
     * 2.处理业务逻辑
     * 3.做出响应
     * @param request
     * @param response
     */
    private void addGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HtttpUtils.getRequestBody(request);
        GoodsBO goodsBO = gson.fromJson(requestBody, (GoodsBO.class));
        goodsService.addGoods(goodsBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 新增商品逻辑图片的业务逻辑
     * commons-fileUpload
     * 1.执行具体得业务逻辑，上传图片
     * 2.响应--抓服务器的响应报文
     * @param request
     * @param response
     */
    private void imgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> map = FileUpLoadUtils.parseRequesment(request);
        String file = (String) map.get("file");
        System.out.println("file:" + file);

        //这里面有一点需要特别注意：
        //服务器上面的路径没有携带域名端口号，那么就表示图片就从当前域名端口去取
        //如果你的图片不在yi个域内，那么应当指明你文件所在的域
        //http://115.29.141.32:8085//static/image/1591111100103打卡时间.jpg
        //file是从Localhost：8085取的，而我们的图片在哪？
        //全路径 http：//localhost:8084
        String domain = (String ) getServletContext().getAttribute("domain");
        System.out.println("domain:"+domain);
        response.getWriter().println(gson.toJson(Result.ok(domain+file)));
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/goods/", "");
        if ("getType".equals(action)){
            getType(request,response);
        }else if ("getGoodsByType".equals(action)){
            getGoodsByType(request,response);
        }else if ("deleteType".equals(action)){
            deleteType(request,response);
        }
    }

    /**
     * 删除一个类目
     * @param request
     * @param response
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) {
        String typeId = request.getParameter("typeId");
        goodsService.deleteType(typeId);
        try {
            response.getWriter().println(gson.toJson(Result.ok()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取某个分类下的商品信息
     * 1.获取分类id
     * 2.数据查询
     * 3.按照一定的数据结构返回
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        //来一波校验查询
        List<TypeGoodsVO> goodsVOS = goodsService.getGoodsByType(typeId);
        response.getWriter().println(gson.toJson(Result.ok(goodsVOS)));
    }

    /**
     * 加载商品的所有分类
     * 1.没有请求参数
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
    List<Type> typeList = goodsService.getType();
    response.getWriter().println(gson.toJson(Result.ok(typeList)));
    }
}
